var ExpenseAddPage = {
	'onSaveSucc': function(data, status, xhr) {
		$.messager.info(data.message);
		var mWindow = MsgBox.getCurrentOpenDialogWindow();
		mWindow.datatable.refresh(false);
		MsgBox.closeActiveDialog();
	},
	'onFindOneSucc': function(data, status, xhr) {
		$("#expenseAddFrm").autofill(data.info, {
			'triggerPropertyChange': ['isInstalment']
		});
	},
	'onIsInstalmentValueChanged': function() {
		var isInstalment = $("#expenseAddFrm").find("select[name='isInstalment']").val();
		if (isInstalment == '01') {
			// 分期
			$("#expenseAddFrm").find("[data-tag='instalment-info']").show();
			$("#expenseAddFrm").find("input[name='consumeAmount']").removeAttr("disabled");
		}
		if (isInstalment == '02') {
			$("#expenseAddFrm").find("[data-tag='instalment-info']").hide();
			$("#expenseAddFrm").find("input[name='consumeAmount']").attr("disabled", "disabled");
		}
	},
	'save': function() {
		if ($('#expenseAddFrm').formValidate()) {
			var obj = $('#expenseAddFrm').serializeObject();
			if (obj.isInstalment == '01') {
				// 分期商品
				if (StringUtil.isBlank(obj.installNum)) {
					MsgBox.info("分期商品的分期期数不能为空！", null, function() {
						$("#expenseAddFrm").find('[name="installNum"]').focus();
					});
					return ;
				}
				if (StringUtil.isBlank(obj.mthRepayAmount)) {
					MsgBox.info("分期商品的月还款额不能为空！", null, function() {
						$("#expenseAddFrm").find('[name="mthRepayAmount"]').focus();
					});
					return ;
				}
				if (StringUtil.isBlank(obj.mthRepayDate)) {
					MsgBox.info("分期商品的每月还款日不能为空！", null, function() {
						$("#expenseAddFrm").find('[name="mthRepayDate"]').focus();
					});
					return ;
				}
				if (StringUtil.isBlank(obj.firstRepayDate)) {
					MsgBox.info("分期商品的首次还款日不能为空！", null, function() {
						$("#expenseAddFrm").find('[name="firstRepayDate"]').focus();
					});
					return ;
				}
			}
			var $updateTag = $("#expenseAddFrm").find('[name="updateTag"]');
			if ($updateTag.length == 1) {
				var send = function(tag) {
					saveAjax.tempUrl(function(url) {
						return url + tag;
					}).send();
				};
				
				if (obj.isInstalment == '01') {
					MsgBox.confirm("是否重新生成分期信息？", null, function() {
						send('1');
					}, function() {
						send('0');
					}, { 'okBtnText': '是', 'cancelBtnText': '否' });
				} else {
					send('0');
				}
			} else {
				saveAjax.send();
			}
		}
	},
	'onProductAmountValueChanged': function(ele) {
		var val = $(ele).val();
		$("#expenseAddFrm").find("input[name='consumeAmount']").val(val);
	},
	'onConsumeTypeValueChanged': function(ele) {
		var text = $(ele).find("option[value='" + $(ele).val() + "']").text();
		$("#expenseAddFrm").find("input[name='productName']").val(text);
	},
	// 下载附件
	'download': function(item, e, rowNumber) {
		$.download({
			'data':{
				'fileId':item.fileId,
				'download':true
			},
			'url':ctx + "/datum/file/fileDownload"
		});
		ExpenseAddPage.updateDownloadCount(item.fileId, rowNumber); // 更新下载次数
	},
	// 通过超链接下载文件，更新下载次数
	'updateDownloadCount':function(fileId, rowNumber) {
		updateDownloadCountAjax.tempUrl(function(url){
			return url + "/" + fileId + "/" + rowNumber;
		});
		updateDownloadCountAjax.send();
	},
	// 更新下载次数成功
	'onUpdateDownloadCountSucc':function(data, status, xhr) {
		var obj = data.info; // obj.fileId, obj.row
		var row = obj.row;
		var $downloadCount = $("#expenseFileDatatable").find("tr[row-index='" + row + "']").find("td[column-key='downloadCount'] p");
		$downloadCount.text(parseInt($downloadCount.text()) + 1);
	},
	// 删除附件
	'delete': function(item, e, rowNumber) {
		MsgBox.confirm("确认删除文件：" + item.originalName + "?", null, function() {
			fileDeleteAjax.tempUrl(function(url){
				return url + "/" + item.fileId;
			});
			fileDeleteAjax.send();
		});
	},
	// 文件删除成功
	'onFileDeleteSucc':function(data, status, xhr) {
		$.messager.info(data.message);
		expenseFileDatatable.refresh();
	},
	'onFileChanged': function(ele) {
		
	},
	// 上传成功
	'onUploadSucc':function(stat) {
		stat = JSON.parse(stat);
		if(stat.success == true){
			$.messager.info("文件上传成功！");
			expenseFileDatatable.refresh();
		} else {
			$.messager.error("文件上传失败！");
		}
	},
};